/******************************************************************************
ISYS 1078 / 1079 — Information Retrieval
Assignment #1

Student Name: [ Rui, Wang ]
Student ID: [ 3171459 ]
CSIT username: [ rwang ]
*******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "searchFunc.h"

/*index app
  Used for creating term Lexicon, inverted lists and document Map files, which are used for
  search app
*/

int main(int argc, char **argv)
{
  	HashTable HT;
	DocMap DM;
  	int i;
	FILE *fp_lex, *fp_ivt, *fp_map;

	clock_t start, end, start_s, end_s;

  	if(argc < 5 )
  	{
    	fprintf(stderr, "Usage: %s <lexicon> <invlists> <map> <queryterm-1> [...<queryterm-N]\n",argv[0]);
    	return -1; 
  	}
    
	fp_lex = fopen(argv[1], "rb");
    if(fp_lex == NULL)
    {
      fprintf(stderr,"cannot open source file %s!\n", argv[1]);
      return -1;
    }

	fp_ivt = fopen(argv[2], "rb");
    if(fp_ivt == NULL)
    {
      fprintf(stderr,"cannot open source file %s!\n",argv[2]);
      return -1;
    }

	fp_map = fopen(argv[3], "rb");
    if(fp_map == NULL)
    {
      fprintf(stderr,"cannot open source file %s!\n",argv[3]);
      return -1;
    }
	
  	start = clock();

	BuildSearchMap(&HT, &DM, fp_lex, fp_map);

  	end = clock();

  	fprintf(stdout,"\nHashing time is: %f seconds\n\n",(double)(end-start)/CLOCKS_PER_SEC);

	start_s = clock();
	/*search query iterms*/
	for(i = 4; i < argc; i++)
	{
		Query(argv[i], &HT, &DM, fp_ivt);
		
	}
	
	fclose(fp_lex);
	fclose(fp_map);
    fclose(fp_ivt);

	DestroyHashTable(&HT, &DM);
	
 	end_s = clock();

  	fprintf(stdout,"\nSearching time is: %f seconds\n\n",(double)(end-start)/CLOCKS_PER_SEC);
   	return 0;
}

